package mobile.pubcrawler.ui;

import java.util.List;

import mobile.pubcrawler.Filter;
import mobile.pubcrawler.GPSManager;
import mobile.pubcrawler.MapOverlays;
import mobile.pubcrawler.R;
import mobile.pubcrawler.StartLocationMap;

import com.google.android.maps.GeoPoint;
import com.google.android.maps.MapActivity;
import com.google.android.maps.MapController;
import com.google.android.maps.Overlay;
import com.google.android.maps.OverlayItem;

import android.location.Address;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.RadioGroup.OnCheckedChangeListener;

public class StartLocation extends MapActivity {
		
	 private MapController mapController;
	 private List<Overlay> mapOverlays;
	 private MapOverlays itemizedOverlay;
	 private OverlayItem overlayItem;
	 private StartLocationMap mapView;
	 
	 private RadioGroup group;
	 private RadioButton currentLocation;
	 //private RadioButton selectOnMap;
	 
	 private double latitude;
	 private double longitude;
	 
	 private StartLocation activity;
	 
	 @Override
	 public void onCreate(Bundle savedInstanceState) {
		
		super.onCreate(savedInstanceState);
	    setContentView(R.layout.startlocation);
	    this.setTitle(getString(R.string.startLocationActivityTitle));
	    this.activity = this;
	    
	    mapView = (StartLocationMap)findViewById(R.id.startLocationMapView);
	    mapController = mapView.getController();
        mapView.displayZoomControls(true);
	    mapView.setBuiltInZoomControls(true);
	    mapView.setSatellite(false);
        mapController.setZoom(16);
        
        Button apply = (Button)findViewById(R.id.startLocationApply);
        apply.setOnClickListener(applyButtonListener);
        
        group = (RadioGroup) findViewById(R.id.startLocationRadiogroup);
        currentLocation = (RadioButton) findViewById(R.id.startLocationCurrentLocation);
        //selectOnMap = (RadioButton) findViewById(R.id.startLocationSelectOnMap);
        
        group.setOnCheckedChangeListener(new OnCheckedChangeListener() {
			public void onCheckedChanged(RadioGroup group, int checkedId) {
				if (checkedId == R.id.startLocationCurrentLocation) {
					zoomTo(GPSManager.getInstance().getLatitude(), GPSManager.getInstance().getLongitude());
		   	 		setStartLocation(getGeoPoint(GPSManager.getInstance().getLatitude(), GPSManager.getInstance().getLongitude()));
				}	
			}
        });
        
        if (Filter.getInstance().isRelativeToCurrentPosition()) {
   	 		group.check(R.id.startLocationCurrentLocation);
        	zoomTo(GPSManager.getInstance().getLatitude(), GPSManager.getInstance().getLongitude());
   	 		setStartLocation(getGeoPoint(GPSManager.getInstance().getLatitude(), GPSManager.getInstance().getLongitude()));
   	 	} else {
   	 		group.check(R.id.startLocationSelectOnMap);
   	 		zoomTo(Filter.getInstance().getRelativeToLatitude(), Filter.getInstance().getRelativeToLongitude());
   	 		setStartLocation(getGeoPoint(Filter.getInstance().getRelativeToLatitude(), Filter.getInstance().getRelativeToLongitude()));
   	 	}
   	 	   	 
	 }
	 
	 private OnClickListener applyButtonListener =  new OnClickListener(){
	    	public void onClick(View v) { 
	    		Filter filter = Filter.getInstance();
	    		if (currentLocation.isChecked()) {
	    			filter.setRelativeToCurrentPosition(true);
	    		} else {
	    			Address address = GPSManager.getInstance().getAddress(false, latitude, longitude);
	    			filter.setRelativeToAddress(address);
	    			filter.setRelativeToCurrentPosition(false);
	    			filter.setRelativeToLatitude(latitude);
	    			filter.setRelativeToLongitude(longitude);
	    		}
	    		activity.finish();
	    		filter.setAddressChanged(true);
	    		filter.saveFilterSettings(activity.getApplicationContext());
	    	}
	 };
	 
	 public void setStartLocation(GeoPoint geo) {	
		 mapOverlays = mapView.getOverlays();
		 mapOverlays.clear();
		 overlayItem = new OverlayItem(geo,  "Start location", "");
		 itemizedOverlay = new MapOverlays(this.getResources().getDrawable(R.drawable.standort));
	   	 itemizedOverlay.addOverlay(overlayItem);
	   	 mapOverlays.add(itemizedOverlay);
	   	 mapView.invalidate();
	   	 latitude = geo.getLatitudeE6() / 1E6;
	   	 longitude = geo.getLongitudeE6() / 1E6;
	 }
	 
	 public void mapTouched(GeoPoint geo) {
		 if (group.getCheckedRadioButtonId() == R.id.startLocationSelectOnMap) setStartLocation(geo);
	 }
	 
	 private GeoPoint getGeoPoint(double lat, double lng) {
		Double geoLat = lat*1E6;
	 	Double geoLng = lng*1E6;
	 	return new GeoPoint(geoLat.intValue(), geoLng.intValue()); 
	 }
	 
	 private void zoomTo(double lat, double lng) {
 		mapController.animateTo(getGeoPoint(lat, lng)); 	 
	 }
	 
	 @Override
	 protected boolean isRouteDisplayed() {
		// TODO Auto-generated method stub
		return false;
	 }
	
}